Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgres: create a database with an owner already set #57

Merged
merged 1 commit into from
Nov 16, 2024
Merged

Conversation

malor
Copy link
Member

@malor malor commented Sep 29, 2024

In the current setup databases are owned by the admin role postgres, and we separately grant users the permission to create their own tables in the schema public. I believe our intention was to make users the owners of the corresponding databases, effectively granting them all permissions w/o any extra steps.

This will make the tests and new databases consistent with our production instance where the database is owned by role xsnippet-api.

In the current setup databases are owned by the admin role `postgres`,
and we separately grant users the permission to create their own tables
in the schema `public`. I believe our intention was to make users
owners of the corresponding databases, effectively granting them all
permissions w/o any extra steps.

This will make the tests and new databases consistent with our
production instance where the database is owned by role `xsnippet-api`.
- name: Setup postgresql users
community.postgresql.postgresql_user:
db: "{{ item.database }}"
Copy link
Member Author

@malor malor Sep 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was useless, as the option controls which database to connect to, and not which database to grant permissions on. We connect as admin role postgres to database postgres, and that is sufficient to create user roles and databases.

@@ -42,28 +42,17 @@
enabled: true
become: true

- name: Setup postgresql databases
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to be executed after we create a user, or otherwise setting a database owner will fail due to non-existent role.

user: "{{ item.username }}"
with_items: "{{ postgres_users }}"
become: true
become_user: postgres

- name: Grant users permissions to create tables in the schema `public`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I now think that my fix in 3ccb652 was wrong an I misunderstood what changed in PostgreSQL 15. The database owner will have the necessary permissions on the schema public, and no additional grants are needed (https://www.enterprisedb.com/blog/new-public-schema-permissions-postgresql-15).

@malor malor merged commit f8c9b80 into master Nov 16, 2024
3 checks passed
@malor malor deleted the postgres branch November 16, 2024 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants